package com.quick.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.quick.mapper.ShopGoodsMapper;
import com.quick.shop.doamin.dto.ShopDTO;
import com.quick.shop.doamin.po.ShopGoods;
import com.quick.shop.doamin.vo.ShopGoodsVo;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.shop.doamin.po.Shop;
import com.quick.shop.doamin.search.ShopSearch;
import com.quick.shop.doamin.vo.ShopVo;
import com.quick.base.Page;
import com.quick.base.Search;
import com.quick.mapper.ShopMapper;
import com.quick.service.IShopService;
import com.quick.util.CallBack;
import com.quick.util.Convert;
import com.quick.util.LBSUtil;
import com.quick.util.PageUtil;
import org.springframework.util.Assert;

@Service
public class ShopServiceImpl implements IShopService {
	
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private ShopGoodsMapper shopGoodsMapper;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Page<Shop> search(Search<ShopSearch> search) {
		PageHelper.startPage(search.getPageNum(), search.getPageSize());
		QueryWrapper<Shop> queryWrapper =  new QueryWrapper<Shop>();
		List<Shop> list = shopMapper.selectList(queryWrapper);
		return PageUtil.page(new PageInfo(list),ShopVo.class,new CallBack() {
			@Override
			public List<?> handle(List<?> list) {
				List<ShopVo>  res = Convert.convert(list, ShopVo.class);
				res.forEach(e->{
					ShopSearch where = search.getSearch();
					Assert.hasText(where.getLatitude(),"经度不能为空");
					Assert.hasText(where.getLongitude(),"维度不能为空");
					GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(where.getLatitude()) ,Double.parseDouble(where.getLongitude()));
					GlobalCoordinates target = new  GlobalCoordinates(Double.parseDouble(e.getLongitude()) ,Double.parseDouble(e.getLatitude()));
					e.setDistance(LBSUtil.distance(source, target));
				});
				return res.stream().sorted(Comparator.comparing(ShopVo::getDistance))
		                .collect(Collectors.toList());
			}
		}) ;
	}

	@Override
	public ShopVo queryById(ShopDTO shop) {
		QueryWrapper<Shop> queryWrapper =  new QueryWrapper<Shop>();
		queryWrapper.lambda().eq(Shop::getId,shop.getId());
		Shop result = shopMapper.selectOne(queryWrapper);
		Assert.notNull(result,"店铺不存在");
		ShopVo res = Convert.convert(result,ShopVo.class);
		//TODO 如果需要获取距离最近在这里完成
		//查询店铺商品
		QueryWrapper<ShopGoods> goodsQueryWrapper = new QueryWrapper<>();
		goodsQueryWrapper.lambda().eq(ShopGoods::getShopId,res.getId());
		goodsQueryWrapper.lambda().last("limit 10");
		List<ShopGoods> goods = shopGoodsMapper.selectList(goodsQueryWrapper);
		List<ShopGoodsVo> goodsVos = Convert.convert(goods,ShopGoodsVo.class);
		res.setGoods(goodsVos);
		return res;
	}

}
